package com.csl.seckill.commom;

import lombok.experimental.Accessors;

/**
 * 通用的返回结果
 * @param <T>
 */
@Accessors(chain = true)
public class Result<T> {
   //状态码
    private int code;

    //提示信息
    private String msg;

    //返回数据
    private T data;

    public Result() {
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(ResponseCodeEnum responseCodeEnum,T data){
        this.code=responseCodeEnum.getCode();
        this.msg=responseCodeEnum.getMsg();
        this.data=data;
    }

    public Result(ResponseCodeEnum responseCodeEnum){
        this.code=responseCodeEnum.getCode();
        this.msg=responseCodeEnum.getMsg();
    }

    /**
     * 成功无任何返回
     * @param <T>
     * @return
     */
    public static <T>Result<T> success(){
        return new Result<T>();
    }

    /**
     * 成功返回自定义响应码，不返回数据
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T>Result<T> success(int code,String msg){
        return new Result<T>(code, msg);
    }

    /**
     * 成功返回自定义响应码 及数据
     * @param code
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T>Result<T> success(int code,String msg,T data){
        return new Result<T>(code, msg, data);
    }

    /**
     * 成功并返回数据
     * @param data
     * @param <T>
     * @return
     */
    public static <T>Result<T> success(ResponseCodeEnum responseCodeEnum,T data){
        return new Result<T>(responseCodeEnum,data);
    }


    /**
     * 成功不返回数据
     * @param responseCodeEnum
     * @param <T>
     * @return
     */
    public static <T>Result<T> success(ResponseCodeEnum responseCodeEnum){
        return new Result<T>(responseCodeEnum);
    }

    /**
     * 失败无任何返回
     * @param <T>
     * @return
     */
    public static <T>Result<T> error(){
        return new Result<T>();
    }

    /**
     * 失败返回自定义响应码，不返回数据
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    public static <T>Result<T> error(int code,String msg){
        return new Result<T>(code, msg);
    }

    /**
     * 失败返回自定义响应码 及数据
     * @param code
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T>Result<T> error(int code,String msg,T data){
        return new Result<T>(code, msg, data);
    }


    /**
     * 失败并返回数据
     * @param responseCodeEnum
     * @param data
     * @param <T>
     * @return
     */
    public static <T>Result<T> error(ResponseCodeEnum responseCodeEnum,T data){
        return new Result<>(responseCodeEnum,data);
    }

    /**
     * 失败不返回数据
     * @param responseCodeEnum
     * @param <T>
     * @return
     */
    public static <T>Result<T> error(ResponseCodeEnum responseCodeEnum){
        return new Result<>(responseCodeEnum);
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
